package com.wcl.nullmusic.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.wcl.nullmusic.R;
import com.wcl.nullmusic.base.BaseAppcation;
import com.wcl.nullmusic.mode.Recommend_FocusImages_List;
import com.wcl.nullmusic.ui.activity.AnthologyActivity;
import com.wcl.nullmusic.ui.activity.MusicActivity;
import com.wcl.nullmusic.ui.activity.SongDetailActivity;
import com.wcl.nullmusic.utils.Contant;
import com.wcl.nullmusic.utils.ToActivityUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/5.
 */
public class RecommendViewPagerAdapter extends PagerAdapter{
    private List<Recommend_FocusImages_List> datas;
    private Context context;
   // private List<String> imageViews = new ArrayList<>();


    public RecommendViewPagerAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<Recommend_FocusImages_List> datas) {
        this.datas = datas;
//        for (int i = 0; i < datas.size(); i++) {
//            imageViews.add(datas.get(i).getPic());
//        }
//        imageViews.add(0, datas.get(datas.size() - 1).getPic());
//        imageViews.add(datas.get(0).getPic());
        notifyDataSetChanged();
    }

    //获取当前窗体界面数
    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? Integer.MAX_VALUE : 0;
        //  return datas.size();
    }

    //判断是否由对象生成界面
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    //是从ViewGroup中移出当前View
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        //  super.destroyItem(container, position, object);
    }

    //返回一个对象，这个对象表明了PagerAdapter适配器选择哪个对象放在当前的ViewPager中
    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        ImageView imageView = new ImageView(container.getContext());
//        if (datas != null && datas.size() > 0)
//            temp = position % datas.size();
        Picasso.with(container.getContext()).load(datas.get(position % datas.size()).getPic()).placeholder(R.mipmap.ic_classify_img02).error(R.mipmap.ic_classify_img02).into(imageView);
        container.addView(imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Recommend_FocusImages_List list= datas.get(position%datas.size());
                Bundle bundle = new Bundle();
                switch (datas.get(position%datas.size()).getType()) {
                    case 2:
                        bundle.putString("album", list.getAlbumId()+"");
                        bundle.putString("position", 1 + "");
                       // http://mobile.ximalaya.com/mobile/others/ca/album/track/3556424/true/2/20?device=android
                        ToActivityUtils.startToActivity(context, AnthologyActivity.class, bundle);
                        break;
                    case 3:
                        bundle.putString("url", list.getTrackId()+"");
                        ToActivityUtils.startToActivity(context, MusicActivity.class,bundle);

                        Toast.makeText(context, "跳转到音乐播放", Toast.LENGTH_SHORT).show();
                        break;
                    case 9:
                        bundle.putString("specialId", list.getSpecialId()+"");
                        ToActivityUtils.startToActivity(context, SongDetailActivity.class,bundle);
                        //Toast.makeText(context, "跳转到新页面 :"+ Contant.RECOMMEND_VIEWPAGER_TYPE9_URL+list.getSpecialId()+"", Toast.LENGTH_SHORT).show();

                        break;
                }
            }
        });

        return imageView;
    }


}
